package cn.dancepeak.artcle.controller;

import cn.dancepeak.artcle.service.IArticleServiceImpl;
import cn.dancepeak.article.domain.Article;
import cn.dancepeak.article.query.ArticleQuery;
import cn.dancepeak.exception.BusinessException;
import cn.dancepeak.utils.R;
import cn.dancepeak.vo.ArticleTypeDateNumVo;
import cn.dancepeak.vo.ArticleTypeNameNumVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {
    @Autowired
    private IArticleServiceImpl articleService;

    //1.增
    @PostMapping("/add")
    public R<Void> add(@RequestBody Article article){
        try {
            articleService.add(article);
            return R.ok("新增成功");
        } catch (BusinessException e) {
            log.info("新增用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("新增系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //2.根据ID改
    @PostMapping("/update")
    public R<Void> update(@RequestBody Article article){
        try {
            articleService.update(article);
            return R.ok("修改成功");
        } catch (BusinessException e) {
            log.info("修改用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("修改系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //3.根据ID删
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable("id") Long id){
        try {
            articleService.delete(id);
            return R.ok("删除成功");
        } catch (BusinessException e) {
            log.info("删除用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //4.批量删
    @PostMapping("/deleteByList")
    public R<Void> deleteByList(@RequestBody List<Long> ids){
        try {
            articleService.deleteByList(ids);
            return R.ok("删除成功");
        } catch (BusinessException e) {
            log.info("删除用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //5.根据ID查
    @GetMapping("/{id}")
    public R<Article> getById(@PathVariable("id") Long id){
        try {
            Article article = articleService.getById(id);
            return R.ok(article);
        } catch (BusinessException e) {
            log.info("查询用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //6.分页查询
    @PostMapping("/page")
    public R<PageInfo<Article>> page(@RequestBody ArticleQuery articleQuery){
        try {
            PageInfo<Article> page = articleService.page(articleQuery);
            return R.ok(page);
        } catch (BusinessException e) {
            log.info("查询用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }

    //返回给前端的数据库名称和数量
    @GetMapping("/articleTypeNameNum")
    public R<List<ArticleTypeNameNumVo>> getArticleTypeNameNum(){
        try {
            List<ArticleTypeNameNumVo> list = articleService.getArticleTypeNameNum();
            return R.ok(list);
        } catch (BusinessException e) {
            log.info("查询返回给前端的查询数据库类型名称和数量异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询返回给前端的查询数据库类型名称和数量系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //返回给前端的数据库创建时间和数量
    @GetMapping("/articleTypeDateNum")
    public R<List<ArticleTypeDateNumVo>> getArticleTypeDateNum(){
        try {
            List<ArticleTypeDateNumVo> list = articleService.getArticleTypeDateNum();
            return R.ok(list);
        } catch (BusinessException e) {
            log.info("查询返回给前端的查询数据库日期和数量异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询返回给前端的查询数据库日期和数量系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }

    //查询其他相关文章
    @GetMapping("/relevant/{id}/{articleTag}")
    public R<List<Article>> getRelevant(@PathVariable("id") Long id,@PathVariable("articleTag") String articleTag){
        try {
            List<Article> list = articleService.getRelevant(id,articleTag);
            return R.ok(list);
        } catch (BusinessException e) {
            log.info("查询返回给前端的查询数据库日期和数量异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询返回给前端的查询数据库日期和数量系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }

    //查询文章类型关于名字
    @PostMapping("/{articleName}")
    public R<List<Article>> getArticleByTypeName(@PathVariable("articleName") String articleName){
        try {
            List<Article> article = articleService.getArticleByTypeName(articleName);
            return R.ok(article);
        } catch (BusinessException e) {
            log.info("查询文章类型关于名字异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询文章类型关于名字系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }

    //查询柱状图的点赞数量和前六的文章名称
    @PostMapping("/getArticleNameAndArticleStarNum")
    public R<List<Article>> getArticleNameAndArticleStarNum(){
        try {
            List<Article> article = articleService.getArticleNameAndArticleStarNum();
            return R.ok(article);
        } catch (BusinessException e) {
            log.info("查询柱状图的点赞数量和前六的文章名称异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询柱状图的点赞数量和前六的文章名称系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }

    //查询饼状图的同类型文章阅读数量和前五的文章类型
    @PostMapping("/getArticleTagAndArticleReadSum")
    public R<Map<String, Integer>> getArticleTagAndArticleReadSum(){
        try {
            Map<String, Integer> map = articleService.getArticleTagAndArticleReadSum();
            return R.ok(map);
        } catch (BusinessException e) {
            log.info("查询饼状图的同类型文章阅读数量和前五的文章类型异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询饼状图的同类型文章阅读数量和前五的文章类型系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }

    //进入页面阅读量加一
    @PostMapping("/updateReadAddOne/{id}")
    public R<Void> updateReadAddOne(@PathVariable("id") Long id){
        try {
            articleService.updateReadAddOne(id);
            return R.ok("阅读量增加成功");
        } catch (BusinessException e) {
            log.info("阅读量增加异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("阅读量增加系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
}
